﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;
namespace Sunboxsoft.ASMS.Client.UI.Pickers
{
    public partial class VCustomerPicker : UserControl, IView
    {
        //当前页  
        private int _pageIndex = 0;
        //总共几页
        private int _pageCount = 0;

        public VCustomerPicker()
        {
            InitializeComponent();
            ctlGrid.EmbeddedNavigator.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(EmbeddedNavigator_ButtonClick);
        }

        #region IView 成员
        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("ok", Resources.isOK, "确定", OnOKActionClick))
                .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", OnOKRefreshClick))
                .AddAction(new ActionNode("cancel", Resources.isBack, "取消", OnCancelActionClick));
        }
        public void Active(ViewActiveArgs args)
        {
            RefreshData();
        }
        public void Resume(ViewResumeArgs args)
        {
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("选择客户");
        }
        #endregion

        #region Method
        /// <summary>
        /// 数据绑定
        /// </summary>
        private void RefreshData()
        {
            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                string filter = txtQry.EditValue == null ? null : txtQry.EditValue.ToString();
                PagedResult<WebRef.VCustomer> result = new Service.QueryService().QueryCustomerPagedList(
                    filter,
                    _pageIndex * GetPageLimit(),
                    GetPageLimit());
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        ctlGrid.Paginate<WebRef.VCustomer>(result,
                            out _pageIndex,
                            out _pageCount);
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError("查询失败！");
                    }
                    this.HideMask();
                }));
            });
        }
        private int GetPageLimit()
        {
            return 25;
        }
        #endregion

        #region Actions
        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnOKActionClick(object sender, object e)
        {
            int[] selRows = this.GVData.GetSelectedRows();
            if (selRows == null || selRows.Length == 0)
            {
                return;
            }
            WebRef.VCustomer supplier = GVData.GetRow(selRows[0]) as WebRef.VCustomer;
            UIContext.Current.ViewManager.CloseView(true, supplier);
        }
        /// <summary>
        /// 刷新
        /// </summary>
        private void OnOKRefreshClick(object sender, object e)
        {
            _pageIndex = 0;
            RefreshData();
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnCancelActionClick(object sender, object e)
        {
            UIContext.Current.ViewManager.CloseView(false, null);
        }
        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            e.UpdatePageIndex(_pageCount, ref _pageIndex);
            RefreshData();
        }
        /// <summary>
        /// 双击行选中事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GVData_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 2)
            {
                DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hInfo = GVData.CalcHitInfo(new Point(e.X, e.Y));
                if (hInfo.InRow)
                {
                    if (GVData.FocusedRowHandle > -1)
                    {
                        WebRef.VCustomer supplier = GVData.GetFocusedRow() as WebRef.VCustomer;
                        UIContext.Current.ViewManager.CloseView(true, supplier);
                    }
                }
            }
        }
        #endregion
    }
}
